package cn.itsource.smartcampus.service.impl;

import cn.itsource.smartcampus.domain.Teacher;
import cn.itsource.smartcampus.mapper.TeacherMapper;
import cn.itsource.smartcampus.service.TeacherService;
import cn.itsource.smartcampus.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper tmapper;
    @Override
    public List<Teacher> getAll() {
        return tmapper.getAll();
    }

    @Override
    public int deleteById(Long id) {
        return tmapper.deleteById(id);
    }

    @Override
    @Transactional
    public int insert(Teacher tea) {
        return tmapper.insert(tea);
    }

    @Override
    public Teacher selectById(Long id) {
        return null;
    }

    @Override
    public int updateByIdSelective(Teacher tea) {
        return tmapper.updateByIdSelective(tea);
    }
    /**
     * @Description: 导入老师信息
     * @Author: Neuronet
     * @Date: 2023/9/25 17:38
     * @param file: 要导入的文件
     * @Return:
     **/
    @Override
    public void importExcel(MultipartFile file) {
        try {
            // 1.使用工具类从复杂文件中获取到老师信息集合
            List<Teacher> teachers = ExcelUtil.importExcel(file, Teacher.class);
            // 2.调用批量新增方法，保存所有老师信息
            tmapper.batchAdd(teachers);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
